﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
//#JMJ Create_20241215

namespace IHateWorking.Model
{
    public class Converter_Exist2VisibleOrCollapsed : IValueConverter
    {
        /// <summary>
        /// 通过变量是否存在来返回控件的显示/折叠状态
        /// </summary>
        /// <param name="value">控制控件显示/折叠状态的变量</param>
        /// <param name="targetType"></param>
        /// <param name="parameter">有 Inverse 参数既视为置逆</param>
        /// <param name="culture"></param>
        /// <returns>折叠/显示</returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            bool inverse = false;
            if (!(parameter is null) && parameter.ToString() == "Inverse")
            { inverse = true; }

            try
            {
                return inverse? value is null ? Visibility.Visible : Visibility.Collapsed//置逆：value 不为空则隐藏
                    : value is null ? Visibility.Collapsed : Visibility.Visible;//不置逆：value 不为空则显示
            }
            catch (Exception e)
            {
                string ex = e.ToString();
                return Binding.DoNothing;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return Binding.DoNothing;
        }
    }
}
